<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\Adapter\Adapter as DbAdapter;
use Application\Model\ServicesTable;
use Application\Form\CommandeForm;
use Application\Model\Commande;
use Zend\Mail;

class ServicesadministratifsController extends AbstractActionController
{
	protected $serviceTable;
	
    public function servicesadministratifsAction(){
    	$form = new CommandeForm ();
    
    	return new ViewModel(
    			array(
    		'form' =>$form,
    	)
    	);
    }
    
public  function commandeAction()
{	    	
	$commandeForm = new CommandeForm ();    	
	$request = $this->getRequest ();
	date_default_timezone_set('Europe/Paris');
	
	if ($request->isPost ()) {
		
		
		$commande = new Commande();
		$commandeForm->setInputFilter ( $commande->getInputFilter () );
		$commandeForm->setData ( $request->getPost () );
		
		if ($commandeForm->isValid ()) {		
			
			$datas = $commandeForm->getData() ;
			$nom  = $datas['nom'];
			$prenom  = $datas['prenom'];
			$entreprise  = $datas['entreprise'];
			$from  = $datas['email'];
			$telephone  = $datas['telephone'];
			$typeCommande  = $datas['type_commande'];
			$message = $datas['message'];
			
			$mail = new Mail\Message();
			$mail->setBody($message)
			->setFrom($from, $prenom)
			->addTo('lamisa.burundi@gmail.com', $nom)
			->setSubject('Commande '+$typeCommande);
			
			
			$transport = new Mail\Transport\Sendmail();
			$transport->send($mail);
			 
			
// 			return new ViewModel(array(
// 					'services' =>$nom,
// 					'form' => $form,
// 					'resultat' =>'1',
			
// 			));
			
			
	/*		
			
			$mail = new Mail\Message(); //todo ajouter le nom du sender from session
			$mail->setBody($message)
			->setFrom('somebody@example.com', 'TITI')
			->addTo('tijoscarlos@gmail.com','CARLOS')
			->setSubject($typeCommande);
			
			
			
			//$request = $this->getRequest();
			$transport = new Mail\Transport\Sendmail();
			$transport->send($mail);
			
			

			$nom  = $datas['nom'];
			$prenom  = $datas['prenom'];
			$from  = $datas['email'];
			$mail = new Mail\Message();
			$mail->setBody($nom)
			->setFrom($from, $prenom)
			->addTo('lamisa.burundi@gmail.com', $nom)
			->setSubject($typeCommande);
			
			$fileForm = new InputFileForm ();
			$result ='Votre demande a été envoyé avec succès';
			
			$transport = new Mail\Transport\Sendmail();
			$transport->send($mail);
*/
			return $this->redirect ()->toRoute ( 'servicesadministratifs', array (
					'controller' => 'Servicesadministratifs',
					'action' => 'servicesadministratifs'
			) );
			
		}else{
			//echo 'is not valid';
			
			return $this->redirect ()->toRoute ( 'servicesadministratifs', array (
					'controller' => 'Servicesadministratifs',
					'action' => 'servicesadministratifs'
			) );
			
		}
	}
	
}
  	
    public function getServiceTable()
    {
    	if (!$this->serviceTable) {
    		$sm = $this->getServiceLocator();
    		$config = $sm->get('config');
    		$config = $config['db'];
    		$dbAdapter = new DbAdapter($config);
    		//return $dbAdapter;
    		$table = new ServicesTable($dbAdapter);
    		//$this->userTable = $sm->get($table);
    		$this->serviceTable = $table;
    	}
    	return $this->serviceTable;
    }
}
